package qshell

import (
	"errors"
	"fmt"
	"runtime/debug"
	"strings"
)

// try 方法
func try(f func()) error {
	var errs error
	defer func() {
		if err := recover(); err != nil {
			fmt.Printf("%s", debug.Stack())
			// errs = err.(error)
		}
	}()
	f()
	return errs
}

// trylog 方法
func trylog(f func(), ctx Context) {
	defer func() {
		if err := recover(); err != nil {
			ctx.Printf("%s", debug.Stack())
		}
	}()
	f()
}

// formatArgStr 解析输入参数
func formatArgStr(res string) (string, []string, error) {
	args := []string{}
	cmdStr := ""
	// 解析输入参数
	splitL := strings.Split(res, " ")
	for i, value := range splitL {
		if value != " " && value != "" {
			cmdStr = splitL[i]
			if len(splitL) > i {
				args = splitL[i+1:]
			}
			return cmdStr, args, nil
		}
	}
	return cmdStr, args, errors.New("")
}
